﻿using AppDataStruct;
using ComUIControl;
using EECMISWS;
using KWindow.KPageCore;
using KXmlUIControl.KControl;
using LogLib;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace YSLPages
{
    public partial class PageApplyGAQZ : PageControlBase
    {
      
        private PreApplyInfo _appInfo = null;

        private List<KCheckBox> listHKGQZLXCheckBoxs = new List<KCheckBox>();

        private List<KCheckBox> listMACQZLXCheckBoxs = new List<KCheckBox>();

        private KCheckBox kCheckBoxXG = null;

        private KCheckBox kCheckBoxAM = null;

        /// <summary>
        /// 香港签注信息
        /// </summary>
        private LabelApplyInfo labelInfoHKG = null;

        /// <summary>
        /// 澳门签注信息
        /// </summary>
        private LabelApplyInfo labelInfoMAC = null;

        private string _error = "";


        private KCheckBox kCheckBoxAMTQ = null;

        private KCheckBox kCheckBoxXGTQ = null;

        private Button buttonOK = null;
        private Button buttonCancell = null;

        private TextBox textBoxGAID = null;
        private TextBox textBoxRelativeSex = null;
        private TextBox textBoxRelativeName = null;
        private TextBox textBoxLXZID = null;
        private TextBox textBoxGX = null;

        public PageApplyGAQZ()
        {           
            InitializeComponent();
            SetProcessBase(ProcessApplyGAQZ.Instance);
        }

        private void GetControls()
        {
            kCheckBoxXG = GetControl<KCheckBox>("kCheckBoxXG") as KCheckBox;
            kCheckBoxAM = GetControl<KCheckBox>("kCheckBoxAM") as KCheckBox;
            textBoxGX = GetControl<TextBox>("textBoxGX") as TextBox;
            textBoxRelativeSex = GetControl<TextBox>("textBoxRelativeSex") as TextBox;
            textBoxRelativeName = GetControl<TextBox>("textBoxRelativeName") as TextBox;
            textBoxLXZID = GetControl<TextBox>("textBoxLXZID") as TextBox;
            textBoxGAID = GetControl<TextBox>("textBoxGAID") as TextBox;
            kCheckBoxAMTQ = GetControl<KCheckBox>("kCheckBoxAMTQ") as KCheckBox;
            kCheckBoxXGTQ = GetControl<KCheckBox>("kCheckBoxXGTQ") as KCheckBox;
            buttonOK = GetControl<Button>("buttonOK") as Button;
            buttonCancell = GetControl<Button>("buttonCancell") as Button;
        }

        //初始化窗体数据
        public override void InitCtrls()
        {
            InitKeyboards();
            base.InitCtrls();
            GetControls();
            GetQZLXCheckBoxs();           
            SetButtonDefaultStyles(buttonOK);
            SetButtonDefaultStyles(buttonCancell);
        }

        //初始化窗体数据
        public override void InitData()
        {
            base.InitData();
            _appInfo = Process.GetValue<PreApplyInfo>("PreApplyInfo");
            if (_appInfo != null)
            {
                labelInfoHKG = _appInfo.SCZSQ.GetLabelInfo("HKG");
                labelInfoMAC = _appInfo.SCZSQ.GetLabelInfo("MAC");
                InitCheckBoxs();
                InitTextBoxs();
            }
            if (_appInfo == null)
            {
                _appInfo = new PreApplyInfo();
            }
            if(!kCheckBoxXG.Checked && !kCheckBoxAM.Checked)
            {
                Speak("请选择前往地区和签注种类");
            }
        }

        private void InitTextBoxs()
        {
            if(_appInfo == null)
            {
                return;
            }
            CodeMapService service = CodeMapService.Instance;
            if (_appInfo.SCZSQ.SFQZ.Code == "1" && _appInfo.SCZSQ.SCZQZ.Count > 0)
            {
                LabelApplyInfo HKG_QZ = _appInfo.SCZSQ.GetLabelInfo("HKG");
                if(HKG_QZ != null)
                {
                    if(HKG_QZ.QZZL.Code == "11")
                    {
                        textBoxGAID.Text = _appInfo.SCZSQ.QSSFZH;
                        textBoxRelativeSex.Text = _appInfo.SCZSQ.QSXB.Value;
                        textBoxRelativeName.Text = _appInfo.SCZSQ.QSXM;
                        textBoxLXZID.Text = _appInfo.SCZSQ.LXZJHM;
                        textBoxGX.Text = _appInfo.SCZSQ.QSGX.Value;
                    }
                }
                LabelApplyInfo MAC_QZ = _appInfo.SCZSQ.GetLabelInfo("MAC");
                if (MAC_QZ != null)
                {
                    if (MAC_QZ.QZZL.Code == "11")
                    {
                        textBoxGAID.Text = _appInfo.SCZSQ.QSSFZH;
                        textBoxRelativeSex.Text = _appInfo.SCZSQ.QSXB.Value;
                        textBoxRelativeName.Text = _appInfo.SCZSQ.QSXM;
                        textBoxLXZID.Text = _appInfo.SCZSQ.LXZJHM;
                        textBoxGX.Text = _appInfo.SCZSQ.QSGX.Value;
                    }
                }
            }
        }

        private void UpdateQSGXInfo()
        {
            if(kCheckBoxAMTQ.Checked || kCheckBoxXGTQ.Checked)
            {
                CodeMapService service = CodeMapService.Instance;
                _appInfo.SCZSQ.QSSFZH = textBoxGAID.Text;
                _appInfo.SCZSQ.QSXB.Value = textBoxRelativeSex.Text;
                _appInfo.SCZSQ.QSXB.Code = service.FindCode("XB", textBoxRelativeSex.Text);
                _appInfo.SCZSQ.QSXM = textBoxRelativeName.Text;
                _appInfo.SCZSQ.LXZJHM = textBoxLXZID.Text;
                _appInfo.SCZSQ.QSGX.Value = textBoxGX.Text;
                _appInfo.SCZSQ.QSGX.Code = service.FindCode("QSGX", textBoxGX.Text);
            } 
        }

        private bool IsQWDQZCheckVaild(KCheckBox qwdBox, List<KCheckBox> listQZLX, string qwd)
        {
            if(!qwdBox.Checked)
            {
                return false;
            }
            int count = 0;
            KCheckBox checkedBox = null;
            foreach (KCheckBox box in listQZLX)
            {
                if (box.Checked)
                {
                    checkedBox = box;
                    count++;
                }
            }
            if (count == 0)
            {
                _error += string.Format("未选择前往{0}的签注类型", qwd);
                Log.WriteAsync(_error);
                return false;
            }
            else if (count > 1)
            {
                _error += string.Format("选择前往{0}选择了多个签注类型.", qwd);
                return false;
            }
            if (IsLongTimeQZ(checkedBox))
            {
                return true;
            }
            
            List<KCheckBox> listQZZL = GetQZZLCheckBoxs(checkedBox);
            count = 0;
            foreach (KCheckBox box in listQZZL)
            {
                if (box.Checked)
                {
                    count++;
                }
            }
            if (count == 0)
            {
                _error += string.Format("前往{0}没有选择签注种类.", qwd);
                return false;
            }
            else if (count > 1)
            {
                _error += string.Format("前往{0}选择了多个签注种类.", qwd);
                return false;
            }
            return true;
        }

        private bool CheckUserCheckedIsOK()
        {
            _error = "";
            if (!kCheckBoxXG.Checked && !kCheckBoxAM.Checked)
            {
                MsgBox.Show("请选择前往地.");
                return false;
            }
            bool bRet1 = true;
            bool bRet2 = true;
            if(kCheckBoxXG.Checked)
            {
                bRet1 = IsQWDQZCheckVaild(kCheckBoxXG, listHKGQZLXCheckBoxs, "香港");                
            }
            if (kCheckBoxAM.Checked)
            {
                bRet2 = IsQWDQZCheckVaild(kCheckBoxAM, listMACQZLXCheckBoxs, "澳门");
            }
            if (!bRet1 && !bRet2)
            {
                MessageBox.Show(_error);
                return false;
            }

            if((kCheckBoxXGTQ.Checked || kCheckBoxAMTQ.Checked) && kCheckBoxXGTQ.Visible)
            {
                if(string.IsNullOrEmpty(textBoxGAID.Text)
                    || string.IsNullOrEmpty(textBoxGX.Text)
                    || string.IsNullOrEmpty(textBoxLXZID.Text)
                    || string.IsNullOrEmpty(textBoxRelativeName.Text)
                    || string.IsNullOrEmpty(textBoxRelativeSex.Text))
                {
                    MsgBox.Show("请完善亲属信息");
                    return false;
                }
            }

            return true;
        }

        private LabelApplyInfo GetQZInfo(KCheckBox qwdBox, List<KCheckBox> listQZLX)
        {
            CodeMapService service = CodeMapService.Instance;
            LabelApplyInfo labelInfo = new LabelApplyInfo();
            labelInfo.QWD.Code = (string)qwdBox.Tag;
            labelInfo.QWD.Value = service["QWD", labelInfo.QWD.Code];

            KCheckBox checkedBox = listQZLX.Find(r => r.Checked);
            labelInfo.QZZL.Code = (string)checkedBox.Tag;
            labelInfo.QZZL.Value = checkedBox.Title;
            if (IsLongTimeQZ(checkedBox))
            {
                labelInfo.YXQX.Code = "N01";
                labelInfo.YXQX.Value = "一年内";
                labelInfo.YXCS.Code = "9";
                labelInfo.YXCS.Value = "多次有效";
                return labelInfo;
            }
            List<KCheckBox> listQZZL = GetQZZLCheckBoxs(checkedBox);
            KCheckBox qzzlBox = listQZZL.Find(r => r.Checked);
            string qzzl = (string)qzzlBox.Tag;
            if (string.IsNullOrEmpty(qzzl))
            {
                MessageBox.Show("无效的签注种类代码.");
                throw new Exception("无效的签注种类代码.");
            }
            string[] array = qzzl.Split('_');
            if (array.Length == 2)
            {
                labelInfo.YXQX.Code = array[0];
                labelInfo.YXQX.Value = service["YXQ", array[0]];
                labelInfo.YXCS.Code = array[1];
                labelInfo.YXCS.Value = service["QZYXCS", array[1]];
                return labelInfo;
            }
            else
            {
                MessageBox.Show("无效的签注种类代码:" + qzzl);
                throw new Exception("无效的签注种类代码:" + qzzl);
            }
        }

        private bool UpdateQZInfo()
        {
            try
            {
                if (kCheckBoxXG.Checked)
                {
                    labelInfoHKG = GetQZInfo(kCheckBoxXG, listHKGQZLXCheckBoxs);
                    _appInfo.SCZSQ.SetLabelInfo(labelInfoHKG);
                }
                if (kCheckBoxAM.Checked)
                {
                    labelInfoMAC = GetQZInfo(kCheckBoxAM, listMACQZLXCheckBoxs);
                    _appInfo.SCZSQ.SetLabelInfo(labelInfoMAC);
                }
                UpdateQSGXInfo();
                _appInfo.SCZSQ.SFQZ.Code = "1";
                _appInfo.SCZSQ.SFQZ.Value = "是";
                return true;
            }
            catch (Exception ex)
            {
                Log.WriteException(ex.ToString());
                MessageBox.Show("更新签注信息发生了异常:" + ex.Message);
                return false;
            }         
        }

        /// <summary>
        /// 清理窗体数据(窗体本次做完后)
        /// </summary>
        public override void ClearData()
        {
            base.ClearData();
            //foreach (KCheckBox box in CheckBoxs)
            //{
            //    box.Checked = false;
            //}
            _appInfo = null;
            labelInfoHKG = null;
            labelInfoMAC = null;
        }
        private void GetQZLXCheckBoxs()
        {
            List<Control> list0 = dicSubGroup[kCheckBoxXG];
            foreach (Control ctl in list0)
            {
                if (ctl is KCheckBox)
                {
                    listHKGQZLXCheckBoxs.Add(ctl as KCheckBox);
                }
            }

            List<Control> list1 = dicSubGroup[kCheckBoxAM];
            foreach (Control ctl in list1)
            {
                if (ctl is KCheckBox)
                {
                    listMACQZLXCheckBoxs.Add(ctl as KCheckBox);
                }
            }
        }

        private bool IsLongTimeQZ(KCheckBox box)
        {
            List<Control> list = dicSubGroup[box];
            if (list == null || list.Count == 0)
            {
                return true;
            }
            return false;
        }

        private List<KCheckBox> GetQZZLCheckBoxs(KCheckBox box)
        {
            List<KCheckBox> listBoxs = new List<KCheckBox>();
            List<Control> list = dicSubGroup[box];
            if (list == null || list.Count == 0)
            {
                return listBoxs;
            }
            foreach (Control ctl in list)
            {
                if (ctl is KCheckBox)
                {
                    listBoxs.Add(ctl as KCheckBox);
                }
            }
            return listBoxs;
        }

        private void InitQZLXCheckbox(LabelApplyInfo info, KCheckBox box, List<KCheckBox> listQZLX)
        {
            if(info == null)
            {
                return;
            }
            box.Checked = true;
            KCheckBox boxChecked = null;
            if (!string.IsNullOrEmpty(info.QZZL.Code))
            {
                int index = listQZLX.FindIndex(r => info.QZZL.Code == (string)r.Tag);
                if (index >= 0)
                {
                    listQZLX[index].Checked = true;
                    boxChecked = listQZLX[index];
                }
                else
                {
                    listQZLX[0].Checked = true;
                    boxChecked = listQZLX[0];
                }
            }

            if (boxChecked != null && !IsLongTimeQZ(boxChecked))
            {
                if (!string.IsNullOrEmpty(info.YXCS.Code)
               && !string.IsNullOrEmpty(info.YXQX.Code))
                {
                    List<KCheckBox> listQZZL = GetQZZLCheckBoxs(boxChecked);
                    string qzzl = info.YXQX.Code + "_" + info.YXCS.Code;
                    int index = listQZZL.FindIndex(r => qzzl == (string)r.Tag);
                    if (index >= 0)
                    {
                        listQZZL[index].Checked = true;
                    }
                }
            }
        }

        private void InitCheckBoxs()
        {
            InitQZLXCheckbox(labelInfoHKG, kCheckBoxXG, listHKGQZLXCheckBoxs);
            InitQZLXCheckbox(labelInfoMAC, kCheckBoxAM, listMACQZLXCheckBoxs);
        }

        /// <summary>
        /// 保存窗体数据(窗体本次做完后, 进入下一步之前)
        /// </summary>
        public override void UpdateData()
        {
            UpdateQZInfo();
        }

        /// <summary>
        /// 窗体关闭时
        /// </summary>
        public override void Close()
        {
            base.Close();
        }

        public override void StartWork()
        {
            base.StartWork();
        }

        public override void StopWork()
        {
            base.StopWork();
        }

        public override void OnCountdownCompleted(object sender, EventArgs e)
        {
            Log.WriteAsync("用户操作超时，返回首页");
            base.OnCountdownCompleted(sender, e);
        }

        protected override void btnBack_Click(object sender, EventArgs e)
        {
            base.btnBack_Click(sender, e);
        }

        protected override void btnNext_Click(object sender, EventArgs e)
        {
            base.btnNext_Click(sender, e);
        }

        protected override void btnCancel_Click(object sender, EventArgs e)
        {
            base.btnCancel_Click(sender, e);
        }

        private void btnOK_Click(object sender, EventArgs e)
        {

            if (!CheckUserCheckedIsOK())
            {
                return;
            }
            StopCountdown();
            UpdateData();
            ClearData();
            Process.ToAny("YSLPages.PageSelectDocumentType");
        }

        private void btnCancell_Click(object sender, EventArgs e)
        {
            StopCountdown();
            ClearData();
            Process.ToAny("YSLPages.PageApplyGAZInfo");
        }
    }
}
